package cn.org.intelli.lims.o2oa.model;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.sub_erupt.Drill;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_erupt.Link;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.ReferenceTableType;

import javax.persistence.*;
import java.util.Date;
import java.util.UUID;

@Getter
@Setter
@Entity
@Table(name = "lims_locker")
@SQLDelete(sql = "update lims_locker set deleted = true, delete_time = now() where id = ?")
@Erupt(name = "耗材存储柜信息", dataProxy = Locker.class, filter = @Filter("Locker.deleted = false"), drills = {@Drill(title = "存储耗材信息", link = @Link(linkErupt = Material.class, joinColumn = "locker.id"))})
public class Locker implements DataProxy<Locker> {
    @Id
    @EruptField(views = @View(title = "标签", type = ViewType.QR_CODE))
    private String id;
    @EruptField(views = @View(title = "名称"), edit = @Edit(title = "名称"))
    private String name;
    @ManyToOne
    @EruptField(views = @View(title = "位置信息", column = "name"), edit = @Edit(title = "位置信息", type = EditType.REFERENCE_TABLE, referenceTableType = @ReferenceTableType(id = "id", label = "name")))
    private Position position;
    @Column(name = "delete_time")
    private Date deleteTime;
    private Boolean deleted = false;

    @Override
    public void beforeAdd(Locker locker) {
        if (locker.getId() == null) {
            locker.setId(UUID.randomUUID().toString());
        }
    }
}
